package com.hopes.bizz.packet.hj212.client;

import com.hopes.bizz.packet.hj212.config.BootNettyChannelInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * @author lyb
 * @version 1.0
 * @description: 模拟hj212数据客户端
 * @date 2023/2/27 14:16
 */
@Component
public class Hj212Client implements ApplicationRunner {

    protected Logger log = LoggerFactory.getLogger(Hj212Client.class);

    private NioEventLoopGroup eventLoopGroup;
    private Bootstrap bootstrap = null;
    private Channel channel;

    @Value("${hj212.server.port:2023}")
    private Integer port;
    @Value("${hj212.server.host:127.0.0.1}")
    private String host;
    @Value("${hj212.worker:8}")
    private Integer workerThread;

    @Autowired
    private BootNettyChannelInitializer bootNettyChannelInitializer;

    @PreDestroy
    private void destroy() {
        if (eventLoopGroup != null) {
            try {
                eventLoopGroup.shutdownGracefully().sync();
            } catch (InterruptedException e) {
                log.error("服务停止失败", e);
            }
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        eventLoopGroup = new NioEventLoopGroup(workerThread);
//        bootstrap = new Bootstrap();
//        bootstrap.group(eventLoopGroup).
//                channel(NioSocketChannel.class).
//                option(ChannelOption.SO_KEEPALIVE, true).
//                remoteAddress(host, port).
//                handler(bootNettyChannelInitializer);
//        try {
//            do {
//                try {
////                    bootstrap.connect(host, port).sync();
//                    log.info("客户端连接服务端成功！连接地址：[{}:{}].", host, port);
//                    break;
//                } catch (Exception e) {
//                    log.error("连接服务端异常!", e);
//                }
//                TimeUnit.MILLISECONDS.sleep(3000);
//                log.info("客户端链路进行重连...");
//            } while (true); //链路断开重试直到成功
//
//        } finally {
//            destroy();
//        }
    }

    @SneakyThrows
    public Channel createChannel() {
        if (bootstrap == null) {
            eventLoopGroup = new NioEventLoopGroup(workerThread);
            bootstrap = new Bootstrap();
            bootstrap.group(eventLoopGroup).
                    channel(NioSocketChannel.class).
                    option(ChannelOption.SO_KEEPALIVE, true).
                    remoteAddress(host, port).
                    handler(bootNettyChannelInitializer);
        }
        return bootstrap.connect(host, port).sync().channel();
    }
}
